package com.lepu.app.fun.tool.bbwy.adapter;

import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UtilityBase;
import com.eyzhs.app.R;
import com.lepu.app.fun.tool.bbwy.bean.BeanToolBbwy;

public class BbwyDataAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	@SuppressWarnings("unused")
	private Context mContext = null;
	private ArrayList<BeanToolBbwy> mArrayList = null;

	public BbwyDataAdapter(Context context, ArrayList<BeanToolBbwy> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.bbwy_activity_main_listitem, null);

			holder = new ViewHolder();

			TextView tagTextView = (TextView) convertView
					.findViewById(R.id.tagTextView);
			TextView timeTextView = (TextView) convertView
					.findViewById(R.id.timeTextView);
			ImageView typeImageView = (ImageView) convertView
					.findViewById(R.id.typeImageView);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.titleTextView);
			TextView infoTextView = (TextView) convertView
					.findViewById(R.id.infoTextView);

			holder.tagTextView = tagTextView;
			holder.timeTextView = timeTextView;
			holder.typeImageView = typeImageView;
			holder.titleTextView = titleTextView;
			holder.infoTextView = infoTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		BeanToolBbwy item = mArrayList.get(position);

		String currentDate = DateUtilBase.stringFromDate(new Date(),
				"dd-MM-yyyy");
		String itemDate = DateUtilBase.stringFromDate(DateUtilBase
				.dateFromString(item.DoItTime, DateUtilBase.DATE_ALL),
				"dd-MM-yyyy");

		if (currentDate.equals(itemDate)) {
			holder.tagTextView.setText("今天");
		} else {
			holder.tagTextView.setText(itemDate);
		}
		
		if(position > 0)
		{
			BeanToolBbwy itemPre = mArrayList.get(position-1);
			String preTime = DateUtilBase.stringFromDate(DateUtilBase
					.dateFromString(itemPre.DoItTime, DateUtilBase.DATE_ALL),
					"dd-MM-yyyy");
			if(itemDate.equals(preTime))
			{
				holder.tagTextView.setVisibility(View.GONE);
			}else{
				holder.tagTextView.setVisibility(View.VISIBLE);
			}
		}else{
			holder.tagTextView.setVisibility(View.VISIBLE);
		}

		String timeString = DateUtilBase.stringFromDate(DateUtilBase
				.dateFromString(item.DoItTime, DateUtilBase.DATE_ALL), "HH:mm");
		holder.timeTextView.setText(timeString);

		if (item.RecordType == 1) {
			holder.typeImageView
					.setImageResource(R.drawable.bbwy_icon_nai_fen_list);
			holder.titleTextView.setText("喂奶");

			if (item.MilkType == 0) {
				holder.infoTextView.setText("");
			} else if (item.MilkType == 1) {
				holder.infoTextView.setText("母乳");
			} else if (item.MilkType == 2) {
				StringBuffer sb = new StringBuffer();
				sb.append("奶粉");
				sb.append(" ");
				sb.append(item.BrandName);
				sb.append(" ");
				sb.append(String.valueOf(item.MilkAmount));
				sb.append("ml");
				
				holder.infoTextView.setText(sb.toString());
			} else {
				holder.infoTextView.setText("");
			}

		} else if (item.RecordType == 2) {
			holder.typeImageView
					.setImageResource(R.drawable.bbwy_icon_bian_bian_list);
			holder.titleTextView.setText("臭臭");

			if (item.ShitStatus == 1) {
				holder.infoTextView.setText("干硬");
			} else if (item.ShitStatus == 2) {
				holder.infoTextView.setText("较硬");
			} else if (item.ShitStatus == 3) {
				holder.infoTextView.setText("正常");
			} else if (item.ShitStatus == 4) {
				holder.infoTextView.setText("粘稠");
			} else if (item.ShitStatus == 5) {
				holder.infoTextView.setText("稀");
			} else {
				holder.infoTextView.setText("");
			}
		} else {
			holder.typeImageView
					.setImageResource(R.drawable.bbwy_icon_sleep_list);
			holder.titleTextView.setText("睡觉");

			StringBuffer sb = new StringBuffer();
			if(item.RecordType == 3)
			{
				//睡着了
				sb.append("宝宝睡着了");
			}else{
				//睡醒了
				sb.append("宝宝醒了");
				
				int sleepTime = UtilityBase.parseInt(item.SleepTime);
				if(sleepTime > 0)
				{
					sb.append("  睡了");
					
					int hour = sleepTime / 60;
					int minute = sleepTime % 60;
					
					if(sleepTime < 60)
					{
						sb.append(String.format("%d分钟",minute));
					}else if(minute == 0){
						sb.append(String.format("%d小时", hour));
					}else{
						sb.append(String.format("%d小时%d分钟", hour,minute));
					}
				}
			}
			holder.infoTextView.setText(sb.toString());
		}

		return convertView;
	}

	private static class ViewHolder {
		TextView tagTextView;
		TextView timeTextView;
		ImageView typeImageView;
		TextView titleTextView;
		TextView infoTextView;
	}
}
